package com.yaoyong.yy_demo.module.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

@Service
public class MailService {

    @Resource
    JavaMailSender javaMailSender;

    @Autowired
    TemplateEngine templateEngine;

    /**
     * 最简单的邮件发送
     */
    public void sendSimpleMail(Email email){
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject(email.getSubject());
        // 设置邮件发送者，这个跟application.yml中设置的要一致
        message.setFrom("1968913843@qq.com");
        // 设置邮件接收者，可以有多个接收者，中间用逗号隔开，以下类似
        message.setTo(email.getTo());
        // 设置邮件抄送人，可以有多个抄送人
        message.setCc(email.getTo());
        // 设置隐秘抄送人，可以有多个
        message.setBcc(email.getTo());
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        message.setText(email.getContent());
        // 发送邮件
        javaMailSender.send(message);
    }


    /**
     * 带附件的邮件
     */
    public void sendAttachFileMail(Email email) {
        try{
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //true表示构建一个可以带附件的邮件对象
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setFrom(email.getFrom());
            mimeMessageHelper.setTo(email.getTo());
            mimeMessageHelper.setSubject(email.getSubject());
            mimeMessageHelper.setText(email.getContent());
            File file = new File(email.getAttachmentPath());
            //第一个参数是自定义的名称，后缀需要加上，第二个参数是文件的位置
            mimeMessageHelper.addAttachment(file.getName(), file);
            javaMailSender.send(mimeMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 带图片的邮件
     */
    public void sendMailWithImg(Email email){
        if (email.getPicPath().length!=email.getPicIds().length){
            System.out.println("发送失败");
            return;
        }
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setTo(email.getTo());
            mimeMessageHelper.setFrom(email.getFrom());
            mimeMessageHelper.setSubject(email.getSubject());
            mimeMessageHelper.setText(email.getContent(),true);
            for (int i = 0; i < email.getPicPath().length; i++) {
                FileSystemResource res = new FileSystemResource(new File(email.getPicPath()[i]));
                //第一个参数指的是html中占位符的名字，第二个参数就是文件的位置
                mimeMessageHelper.addInline(email.getPicIds()[i],res);
            }
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用thymeleaf模板
     */
    public void sendHtmlMail(Email email){
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(email.getFrom());
            mimeMessageHelper.setTo(email.getTo());
            mimeMessageHelper.setText(email.getContent(),true);
            mimeMessageHelper.setSubject(email.getSubject());
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
